<template>
	<view>
		<view class="settle-container">
			<view class="left">
				<label class="radio" @click="changeAllGoodsState">
					<radio color="#c00000" :checked="checkAllGoodsState"></radio>
					<view>全选</view>
				</label>
			</view>
			<view class="middle">
				合计：<text class="price">￥{{checkedTotalPrice}}</text>
			</view>
			<view class="right">
				<view class="btn-settle" @click="settlement">结算({{checkedCount}})</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-settle",
		data() {
			return {
				timer: null,
				seconds: 3
			};
		},
		computed: {
			...mapGetters('my_cart', ['checkedCount', 'checkedTotalPrice', 'total']),
			// addstr 是详细的收货地址
			...mapGetters('my_user', ['addressStr']),
			// token 是用户登录成功之后的 token 字符串
			...mapState('my_user', ['token']),
			...mapState('my_cart', ['cart']),
			checkAllGoodsState() {
				return this.total === this.checkedCount
			}
		},
		methods: {
			...mapMutations('my_cart', ['updateAllGoodsState']),
			...mapMutations('my_user', ['updateRedirectInfo']),
			changeAllGoodsState() {
				this.updateAllGoodsState(!this.checkAllGoodsState)
			},
			// 点击了结算按钮
			settlement() {
				// 1. 先判断是否勾选了要结算的商品
				if (!this.checkedCount) return uni.$showMsg('请选择要结算的商品！')

				// 2. 再判断用户是否选择了收货地址
				if (!this.addressStr) return uni.$showMsg('请选择收货地址！')

				// 3. 最后判断用户是否登录了
				// if (!this.token) return uni.$showMsg('请先登录！')
				if (!this.token) {
					this.seconds = 3
					this.showTips(this.seconds)
					this.timer = setInterval(() => {
						this.seconds--
						if (this.seconds <= 0) {
							clearInterval(this.timer)
							uni.switchTab({
								url: '/pages/my/my',
								success: () => {
									this.updateRedirectInfo({
										from: '/pages/cart/cart',
										openType: 'switchTab'
									})
								}
							})
							return
						}
						this.showTips(this.seconds)
					}, 1000)
				}

				// 4.实现微信支付
				this.payOrder()
			},
			async payOrder() {
				// 配置订=单信息参数
				const orderInfo = {
					// order_price: this.checkedTotalPrice,
					order_price: 0.01,
					consignee_addr: this.addressStr,
					goods: this.cart.filter(item => item.goods_state).map(e => ({
						goods_id: e.goods_id,
						goods_number: e.goods_count,
						goods_price: e.goods_price
					}))
				}
				// 1 发起请求创建订单
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				// 1.3 得到服务器响应的“订单编号”
				const orderNumber = res.message.order_number

				// 2.订单预支付
				// 2.1 发起请求获取订单的支付信息
				const {
					data: res2
				} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
					order_number: orderNumber
				})
				// 2.2 预付订单生成失败
				if (res2.meta.status !== 200) return uni.$showError('预付订单生成失败！')
				// 2.3 得到订单支付相关的必要参数
				const payInfo = res2.message.pay
				// 3.发起微信支付
				// 3.1 调用 uni.requestPayment() 发起微信支付
				const [err, succ] = await uni.requestPayment(payInfo)
				// 3.2 未完成支付
				if (err) return uni.$showMsg('订单未支付！')
				// 3.3 完成了支付，进一步查询支付的结果
				const {
					data: res3
				} = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
					order_number: orderNumber
				})
				// 3.4 检测到订单未支付
				if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
				// 3.5 检测到订单支付完成
				uni.showToast({
					title: '支付完成！',
					icon: 'success'
				})
			},
			showTips(seconds) {
				uni.showToast({
					title: '登陆后进行结算' + seconds + '秒后跳转到登录页!',
					mask: true,
					duration: 1500,
					icon: 'none'
				})
			}
		}
	}
</script>

<style lang="scss">
	.settle-container {
		height: 50px;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		width: 100%;
		border-top: 1px solid #efefef;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.radio {
			display: flex;
			align-items: center;
		}

		.middle {
			.price {
				color: #c00000;
				font-size: 18px;
			}
		}

		.right {
			.btn-settle {
				line-height: 50px;
				min-width: 100px;
				background-color: #c00000;
				text-align: center;
				padding: 0 10px;
				color: white;
			}
		}
	}
</style>
